package com.cby.widget.sample.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.cby.widget.lib.kotlin.*
import com.cby.widget.lib.progress.CbyProgressBar
import com.cby.widget.lib.progress.stateListOf

/**
 * @description: ProgressBarActivity
 *
 * @author ChenBaoyang
 * @date 2021/10/8 15:30
 */
class ProgressBarFragment : Fragment() {

    private lateinit var progressBar: CbyProgressBar
    private lateinit var tvProgress: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return LinearLayout {
            layout_width = match_parent
            layout_height = match_parent
            orientation = vertical

            progressBar = CbyProgressBar {
                layout_width = 300
                layout_height = 30
                margin_top = 20
                margin_start = 12
            }

            tvProgress = TextView {
                layout_width = wrap_content
                layout_height = wrap_content
                margin_start = 12
                textSize = 20f
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        progressBar.apply {
            backgroundRx = 20f
            backgroundRy = 20f
            backgroundColor = "#F5F5F5"
            progressRx = 15f
            progressRy = 15f
            padding = 3f
            progress = stateListOf(
                0..19 to arrayOf(0x8000FFE5, 0x80E7FFAA),
                20..59 to arrayOf(0xFF00FFE5, 0xFFE7FFAA),
                60..79 to arrayOf(0xCCFE579B, 0xCCF9FF19),
                80..100 to arrayOf(0xFFFE579B, 0xFFF9FF19)
            )
            percentage = 10
            postDelayed(runnable, 500)
        }
    }

    val runnable: Runnable = object : Runnable {
        override fun run() {
            progressBar.percentage += 5
            tvProgress.text = progressBar.percentage.toString()
            if (progressBar.percentage < 100) {
                progressBar.postDelayed(this, 200)
            }
        }
    }
}